package com.guaoran.algorithm.binary.search;

/**
 * @Auth gucheng
 * @Desc 找元素峰值的下标
 * https://leetcode.com/problems/find-peak-element
 * @Date 2021/1/7 下午5:37
 */
public class FindPeakElement {
    public static void main(String[] args) {
        System.out.println(findPeak(new int[]{1,2,3,3,5,6,4}));
        System.out.println(findPeak(new int[]{1,2,3,1}));
        System.out.println(findPeak(new int[]{3,1,2}));

    }

    private static int findPeak(int[] nums) {
        if(nums == null || nums.length == 0){
            return 0;
        }
        int start = 0;
        int end = nums.length -1 ;
        int mid;
        while(start + 1 < end){
            mid = start + (end -start)/2;
            if(nums[mid] < nums[mid -1]){
                end = mid;
            }
            else if(nums[mid] < nums[mid +1]){
                start = mid;
            }
            else {
                return mid;
            }
        }
        return nums[start]>nums[end] ? start : end;
    }
}
